home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / components / vlcrenderer.js < prev    next >
Encoding:
Text File  |  2007-11-12  |  9.3 KB  |  326 lines

  1. /*
  2. # Miro - an RSS based video player application
  3. # Copyright (C) 2005-2007 Participatory Culture Foundation
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  18. */
  19.  
  20.  
  21. const VLCRENDERER_CONTRACTID = "@participatoryculture.org/dtv/vlc-renderer;1";
  22. const VLCRENDERER_CLASSID = Components.ID("{F9F01D99-9D3B-4A69-BD5F-285FFD360079}");
  23.  
  24. var pybridge = Components.classes["@participatoryculture.org/dtv/pybridge;1"].
  25.         getService(Components.interfaces.pcfIDTVPyBridge);
  26. var jsbridge = Components.classes["@participatoryculture.org/dtv/jsbridge;1"].
  27.         getService(Components.interfaces.pcfIDTVJSBridge);
  28.  
  29. function writelog(str) {
  30.     Components.classes['@mozilla.org/consoleservice;1']
  31.     .getService(Components.interfaces.nsIConsoleService)    
  32.     .logStringMessage(str);
  33. }
  34.  
  35. function VLCRenderer() { 
  36.   this.scheduleUpdates = false;
  37. }
  38.  
  39. VLCRenderer.prototype = {
  40.   QueryInterface: function(iid) {
  41.     if (iid.equals(Components.interfaces.pcfIDTVVLCRenderer) ||
  42.       iid.equals(Components.interfaces.nsISupports))
  43.       return this;
  44.     throw Components.results.NS_ERROR_NO_INTERFACE;
  45.   },
  46.  
  47.   init: function(window) {
  48.     this.document = window.document;
  49.     var videoBrowser = this.document.getElementById("mainDisplayVideo");
  50.     this.vlc = videoBrowser.contentDocument.getElementById("video1");
  51.     this.timer = Components.classes["@mozilla.org/timer;1"].
  52.           createInstance(Components.interfaces.nsITimer);
  53.     this.timer2 = Components.classes["@mozilla.org/timer;1"].
  54.           createInstance(Components.interfaces.nsITimer);
  55.     this.active = false;
  56.     this.startedPlaying = false;
  57.     this.item = null;
  58.     this.playTime = 0;
  59.     this.volume = 0;
  60.   },
  61.  
  62.   doScheduleUpdates: function() {
  63.       var callback = {
  64.       notify: function(timer) { this.parent.updateVideoControls()}
  65.       };
  66.       callback.parent = this;
  67.       this.timer.initWithCallback(callback, 500,
  68.                   Components.interfaces.nsITimer.TYPE_ONE_SHOT);
  69.   },
  70.  
  71.   updateVideoControls: function() {
  72.     try {
  73.       var elapsed = 0;
  74.       var len = 1;
  75.       if (this.active) {
  76.       if(this.vlc.playlist.isPlaying) {
  77.           this.startedPlaying = true;
  78.           elapsed = this.vlc.input.time;
  79.           len = this.vlc.input.length;
  80.           if (len < 1) len = 1;
  81.           if (elapsed < 0) elapsed = 0;
  82.           if (elapsed > len) elapsed = len;
  83.       } else if (this.startedPlaying) {
  84.           // hit the end of the playlist
  85.             this.active = false;
  86.           this.scheduleUpdates = false;
  87.           pybridge.onMovieFinished();
  88.       }
  89.   
  90.       var progressSlider = this.document.getElementById("progress-slider");
  91.       if(!progressSlider.beingDragged) {
  92.           jsbridge.setSliderText(elapsed);
  93.           jsbridge.setDuration(len);
  94.           jsbridge.moveSlider(elapsed/len);
  95.       }
  96.       }
  97.       if(this.scheduleUpdates) {
  98.       this.doScheduleUpdates();
  99.  
  100.       }
  101.     } catch (e) {
  102.       if (this.startedPlaying) {
  103.     // probably hit the end of the playlist in the middle of this function
  104.         this.scheduleUpdates = false;
  105.         this.active = false;
  106.     pybridge.onMovieFinished();
  107.       } else if(this.scheduleUpdates) {
  108.       this.doScheduleUpdates();
  109.       }
  110.     }
  111.   },
  112.  
  113.   resetVideoControls: function () {
  114.      jsbridge.setSliderText(0);
  115.      jsbridge.setDuration(-1);
  116.      jsbridge.moveSlider(0);
  117.   },
  118.  
  119.   showPauseButton: function() {
  120.     var playButton = this.document.getElementById("bottom-buttons-play");
  121.     playButton.className = "bottom-buttons-pause";
  122.     var playMenuItem = this.document.getElementById('menuitem-playpausevideo');
  123.     var label = new Object();
  124.     pybridge.getLabel("PlayPauseVideo", "pause",0 ,0 ,0,label);
  125.     playMenuItem.label = label.value;
  126.   },
  127.  
  128.   showPlayButton: function() {
  129.     var playButton = this.document.getElementById("bottom-buttons-play");
  130.     playButton.className = "bottom-buttons-play";
  131.     var playMenuItem = this.document.getElementById('menuitem-playpausevideo');
  132.     var label = new Object();
  133.     pybridge.getLabel("PlayPauseVideo", "play",0 ,0 ,0,label);
  134.     playMenuItem.label = label.value;
  135.   },
  136.  
  137.   reset: function() {
  138.       // We don't need these, and stops seem to cause problems, so I'm
  139.       // commenting them out --NN
  140.       // this.stop();
  141.       // this.vlc.playlist.items.clear();
  142.       this.showPlayButton();
  143.       this.resetVideoControls();
  144.   },
  145.  
  146.   canPlayURL: function(url) {
  147.     try {
  148.       if (this.vlc.playlist.items.count > 0) {
  149.           this.stop();
  150.           this.vlc.playlist.items.clear();
  151.       }
  152.       this.item = this.vlc.playlist.add(url);
  153.     } catch (e) {
  154.       return false;
  155.     }
  156.     return true;
  157.   },
  158.  
  159.   selectURL: function(url) {
  160.       if (this.vlc.playlist.items.count > 0) {
  161.           this.stop();
  162.           this.vlc.playlist.items.clear();
  163.       }
  164.       this.item = this.vlc.playlist.add(url);
  165.   },
  166.  
  167.   setCurrentTime: function(time) {
  168.       try {
  169.       this.vlc.input.time = time * 1000;
  170.       } catch (e) {
  171.       var callback = {
  172.           notify: function(timer) {
  173.           this.parent.setCurrentTime(this.parent.playTime);
  174.           }
  175.       };
  176.       callback.parent = this;
  177.       this.playTime = time;
  178.       this.timer2.initWithCallback(callback, 10,
  179.                        Components.interfaces.nsITimer.TYPE_ONE_SHOT);
  180.       }
  181.     },
  182.   
  183.   play: function() {
  184.       if (this.vlc.playlist.items.count > 0) {
  185.       if(!this.vlc.playlist.isPlaying) {
  186.           if (this.item != null) {
  187.           this.vlc.playlist.playItem(this.item);
  188.           this.item = null;
  189.           } else {
  190.           this.vlc.playlist.play();
  191.           }
  192.       } 
  193.       this.scheduleUpdates = true;
  194.       this.active = true;
  195.       this.startedPlaying = false;
  196.       this.doScheduleUpdates();
  197.       this.showPauseButton();
  198.       } else {
  199.       this.active = false;
  200.       this.scheduleUpdates = false;
  201.       pybridge.onMovieFinished();
  202.       }
  203.   },
  204.  
  205.   playFromTime: function(time) {
  206.       this.play();
  207.       this.setCurrentTime(time);
  208.   },
  209.  
  210.   pause: function() {
  211.       this.scheduleUpdates = false;
  212.       this.active = false;
  213.       if (this.vlc.playlist.isPlaying) {
  214.       if (this.vlc.playlist.items.count > 0) {
  215.           this.vlc.playlist.togglePause();
  216.       }
  217.       }
  218.       this.showPlayButton();
  219.   },
  220.  
  221.   pauseForDrag: function() {
  222.       this.scheduleUpdates = false;
  223.       this.active = false;
  224.       if (this.vlc.playlist.isPlaying) {
  225.       if (this.vlc.playlist.items.count > 0) {
  226.           this.vlc.playlist.togglePause();
  227.       }
  228.       }
  229.   },
  230.  
  231.   stop: function() {
  232.       this.scheduleUpdates = false;
  233.       this.active = false;
  234.       if (this.vlc.playlist.items.count > 0) {
  235.       this.vlc.playlist.stop();
  236.       }
  237.       this.showPlayButton();
  238.       this.resetVideoControls();
  239.   },
  240.  
  241.   goToBeginningOfMovie: function() {
  242.     this.setCurrentTime(0);
  243.   },
  244.  
  245.   getDuration: function() {
  246.     try {
  247.       rv = this.vlc.input.length;
  248.     } catch (e) {
  249.       rv = -1;
  250.     }
  251.     return rv;
  252.   },
  253.  
  254.   getCurrentTime: function() {
  255.       var rv;
  256.       rv = this.vlc.input.time;
  257.       return rv / 1000.0;
  258.   },
  259.  
  260.   setVolume: function(level) {
  261.       this.volume = level * 200;
  262.       if (!this.extractMode) {
  263.       this.vlc.audio.mute = false;
  264.       this.vlc.audio.volume = this.volume;
  265.       }
  266.   },
  267.  
  268.   goFullscreen: function() {
  269.     this.vlc.video.fullscreen = true;
  270.   },
  271.  
  272.   extractMovieData: function (url, screenshot_filename) {
  273.       // Disabled until the external helper application works.
  274.       pybridge.extractFinish (-1, false);
  275.   },
  276. };
  277.  
  278. var Module = {
  279.   _classes: {
  280.       VLCRenderer: {
  281.           classID: VLCRENDERER_CLASSID,
  282.           contractID: VLCRENDERER_CONTRACTID,
  283.           className: "DTV VLC Renderer",
  284.           factory: {
  285.               createInstance: function(delegate, iid) {
  286.                   if (delegate)
  287.                       throw Components.results.NS_ERROR_NO_AGGREGATION;
  288.                   return new VLCRenderer().QueryInterface(iid);
  289.               }
  290.           }
  291.       }
  292.   },
  293.  
  294.   registerSelf: function(compMgr, fileSpec, location, type) {
  295.       var reg = compMgr.QueryInterface(
  296.           Components.interfaces.nsIComponentRegistrar);
  297.  
  298.       for (var key in this._classes) {
  299.           var c = this._classes[key];
  300.           reg.registerFactoryLocation(c.classID, c.className, c.contractID,
  301.                                       fileSpec, location, type);
  302.       }
  303.   },
  304.  
  305.   getClassObject: function(compMgr, cid, iid) {
  306.       if (!iid.equals(Components.interfaces.nsIFactory))
  307.           throw Components.results.NS_ERROR_NO_INTERFACE;
  308.  
  309.       for (var key in this._classes) {
  310.           var c = this._classes[key];
  311.           if (cid.equals(c.classID))
  312.               return c.factory;
  313.       }
  314.  
  315.       throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  316.   },
  317.  
  318.   canUnload: function (aComponentManager) {
  319.       return true;
  320.   }
  321. };
  322.  
  323. function NSGetModule(compMgr, fileSpec) {
  324.   return Module;
  325. }
  326.